package com.hashwolf.newpipe;

import android.app.Application;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.preference.PreferenceManager;

import com.hashwolf.newpipe.settings.NewPipeSettings;

import org.schabi.newpipe.extractor.downloader.Downloader;

public class App extends Application {
    public static final String PACKAGE_NAME = BuildConfig.APPLICATION_ID;
    private static final String TAG = App.class.toString();

    private boolean isFirstRun = false;
    private static App app;

    @NonNull
    public static App getApp() {
        return app;
    }

    public boolean isFirstRun() {
        return isFirstRun;
    }

//    private static DownloaderImpl instance;
//
//    /**
//     * It's recommended to call exactly once in the entire lifetime of the application.
//     *
//     * @param builder if null, default builder will be used
//     * @return a new instance of {@link DownloaderImpl}
//     */
//    public static DownloaderImpl init(@Nullable final OkHttpClient.Builder builder) {
//        instance = new DownloaderImpl(
//                builder != null ? builder : new OkHttpClient.Builder());
//        return instance;
//    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "onCreate: ");

        // TODO: ProcessPhoenix重启逻辑

        // 获取版本号和判断是否第一次运行app
        final int lastUsedPrefVersion = PreferenceManager.getDefaultSharedPreferences(this).getInt(getString(R.string.last_used_preferences_version), -1);
        isFirstRun = lastUsedPrefVersion == -1;

        // 首次运行则初始化配置因为其他的初始化可能使用这里的值
        NewPipeSettings.initSettings(this);
    }

//    protected Downloader getDownloader() {
//        final DownloaderImpl downloader = DownloaderImpl.init(null);
//        setCookiesToDownloader(downloader);
//        return downloader;
//    }
}
